package com.dao;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;
import java.util.List;

public class AdminDao {
    public AdminInfo login(String adminName, String password) {
        return DBUtil.getSingleObj("select * from adminInfo where adminName=? and password=? and state!=0 ",
                AdminInfo.class, adminName,password);
    }

    public AdminInfo getAdminNamebyid(String name) {
        return DBUtil.getSingleObj("select * from admininfo where adminname=?",AdminInfo.class,name);
    }

    public int addAdmin(AdminInfo admin) {
        return DBUtil.addAndReturnId("insert into admininfo (adminname,password,note,state) values (?,?,?,?)",
                admin.getAdminName(), admin.getPassword(),admin.getNote(),admin.getState());
    }

    public static int getAdminCount() {
        long i= DBUtil.getScalar("select count(*) from admininfo");
        return (int)i;
    }

    public static List<AdminInfo> getAdminList(PageInfo page) {
        return DBUtil.getList("select * from admininfo limit ?,?",AdminInfo.class,page.getBeginRow(),page.getPageSize());
    }

    public int del(int id) {
        return DBUtil.update("update admininfo set state=0 where id=?",id);
    }

    public int suo(int id) {
        return DBUtil.update("update admininfo set state=2 where id=?",id);
    }
    public int jie(int id) {
        return DBUtil.update("update admininfo set state=1 where id=?",id);
    }

    public int update(AdminInfo adminInfo) {
        return DBUtil.update("update admininfo set adminName=?,note=? where id=?",adminInfo.getAdminName(),adminInfo.getNote(),adminInfo.getId());
    }

    public static AdminInfo getAdminNamebyId(int id) {
        return DBUtil.getSingleObj("select * from admininfo where id=?",AdminInfo.class,id);
    }

    public AdminInfo checkpsw(String psw) {
        return DBUtil.getSingleObj("select * from adminInfo where password=? and state!=0 ",
                AdminInfo.class,psw);
    }

    public int updatepsw(AdminInfo adminInfo) {
        return DBUtil.update("update admininfo set password=? where adminname=?",adminInfo.getPassword(),adminInfo.getAdminName());
    }
}
